package com.hospital.register.order.task;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.register.dto.order.OrderPageDto;
import com.hospital.register.enums.OrderStatusEnum;
import com.hospital.register.order.service.OrderInfoService;
import com.hospital.register.vo.order.OrderQueryVo;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class OrderTask {

    @Autowired
    private OrderInfoService orderInfoService;

    private Logger logger = LoggerFactory.getLogger(OrderTask.class);

    /**
     * 就医提醒，未实现
     * 因为没有注册 sms 短信模板
     */
    //每天8点执行方法，就医提醒
    //0 0 8 * * ?
    @Scheduled(cron = "0/30 * * * * ?")
    public void medicalAdvice() {
        Page<OrderQueryVo> pageConditions = new Page<>(1L, 10L);

        String today = new DateTime().toString("yyyy-MM-dd");
        OrderPageDto orderConditions = new OrderPageDto();
        orderConditions.setReserveDate(today);
        orderConditions.setOrderStatus(OrderStatusEnum.PAID.getStatus());

        Page<OrderQueryVo> resultPage = this.orderInfoService.selectPage(pageConditions, orderConditions);
        logger.info("就医提醒，检测到下列待就诊订单：" + resultPage.getRecords().toString());

        while (resultPage.hasNext()) {
            pageConditions.setCurrent(pageConditions.getCurrent() + 1L);
            resultPage = this.orderInfoService.selectPage(pageConditions, orderConditions);
            logger.info("就医提醒，检测到下列待就诊订单：" + resultPage.getRecords().toString());
        }

        //通知未支付的
        pageConditions.setCurrent(1L);
        pageConditions.setSize(10L);
        orderConditions.setOrderStatus(OrderStatusEnum.UNPAID.getStatus());

        resultPage = this.orderInfoService.selectPage(pageConditions, orderConditions);
        logger.info("就医提醒，检测到下列待支付订单：" + resultPage.getRecords().toString());

        while (resultPage.hasNext()) {
            pageConditions.setCurrent(pageConditions.getCurrent() + 1L);
            resultPage = this.orderInfoService.selectPage(pageConditions, orderConditions);
            logger.info("就医提醒，检测到下列待支付订单：" + resultPage.getRecords().toString());
        }

    }

}
